﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Guline.wsjx.Models;
using System.Configuration;
using System.Data;
namespace Guline.wsjx
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://wsjs.guline.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WsjxServer : System.Web.Services.WebService
    {
        private CommonFunction common;
        private string GulineKey = "8c2479a95037251ecf8ebbdb0ba1238c";
        public WsjxServer()
        {
            common = new CommonFunction(ConfigurationManager.AppSettings["CSDL"]);
        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World" + HttpContext.Current.Request.UserHostAddress;
        }
        [WebMethod]
        public bool Register(string gulinekey,string username, string password1, string password2, string email, string quest, string answer, string phone)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.register(username,password1,password2,email,quest,answer,phone);
        }
        [WebMethod]
        public bool Login(string gulinekey,string username,string password)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.Login(username, password);
        }
        [WebMethod]
        public bool Checkuser(string gulinekey,string username)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.checkUser(username);
        }
        [WebMethod]
        public bool CheckEmail(string gulinekey,string email)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.checkEmail(email);
        }
        [WebMethod]
        public bool changpass1(string gulinekey,string username, string pass1, string quest, string answer)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.changpass1(username,pass1,quest,answer);
        }
        [WebMethod]
        public bool changpass2(string gulinekey,string username, string pass2,string pass2new)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.changpass2(username, pass2,pass2new);
        }
        [WebMethod]
        public bool change_email(string gulinekey, string username, string pass2, string email, string quest, string answer)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.change_email(username, pass2,email,quest,answer);
        }
        [WebMethod]
        public bool change_quest(string gulinekey, string username, string pass2, string email, string quest, string answer)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.change_questanwser(username, pass2, email, quest, answer);
        }
        //string username, string pass1, string pass2, string email, string nextpoint, string nextpoint1, string nextpoint2, string nextpoint3, string nextpoint4, string nextpoint5, string nextpoint6, string nextpoint7
        [WebMethod]
        public bool edit_Info(string gulinekey, string question, string answer, string phone, bool banned, string username, string pass1, string pass2, string email, string nextpoint, string nextpoint1, string nextpoint2, string nextpoint3, string nextpoint4, string nextpoint5, string nextpoint6, string nextpoint7)
        {
            if (gulinekey != this.GulineKey)
            {
                return false;
            }
            return common.editinfo(question,answer,phone,banned,username,pass1,pass2,email,nextpoint,nextpoint1,nextpoint2,nextpoint3,nextpoint4,nextpoint5,nextpoint6,nextpoint7);
        }
        [WebMethod]
        public int CongKNB(string gulinekey,string username,int howmany)
        {
            if (gulinekey != this.GulineKey)
            {
                return -1;
            }
            return common.setCongKNB(username,howmany);
        }
        [WebMethod]
        public int SetKNB(string gulinekey,string username, int howmany)
        {
            if (gulinekey != this.GulineKey)
            {
                return -1;
            }
            return common.setKNB(username, howmany);
        }
        [WebMethod]
        public Account_Info getAccountInfo(string gulinekey,string username)
        {
            try
            {
                if (gulinekey != this.GulineKey)
                {
                    return null;
                }
                Account_Info info = null;
                DataTable t = common.getAccountInfo(username);
                DataTable tsub = common.getAccountSubInfo(username);
                if (t != null)
                {
                    info = new Account_Info();
                    info.bIsBanned = bool.Parse(t.Rows[0]["bIsBanned"].ToString());
                    info.bIsUseOTP = bool.Parse(t.Rows[0]["bIsUseOTP"].ToString());
                    info.bParentalControl = bool.Parse(t.Rows[0]["bParentalControl"].ToString());
                    info.cAccName = t.Rows[0]["cAccName"].ToString();
                    if (t.Rows[0]["dLoginDate"].ToString() == null)
                    {

                        info.dLoginDate = null;
                    }
                    else
                    {
                        info.dLoginDate = DateTime.Parse(t.Rows[0]["dLoginDate"].ToString());
                    }
                    if (t.Rows[0]["dLogoutDate"].ToString() == null)
                    {

                        info.dLogoutDate = null;
                    }
                    else
                    {
                        info.dLogoutDate = DateTime.Parse(t.Rows[0]["dLogoutDate"].ToString());
                    }
                    info.nExtPoint = Convert.ToInt16(t.Rows[0]["nExtPoint"].ToString());
                    info.nExtPoint1 = Convert.ToInt16(t.Rows[0]["nExtPoint1"].ToString());
                    info.nExtPoint2 = Convert.ToInt16(t.Rows[0]["nExtPoint2"].ToString());
                    info.nExtPoint3 = Convert.ToInt16(t.Rows[0]["nExtPoint3"].ToString());
                    info.nExtPoint4 = Convert.ToInt16(t.Rows[0]["nExtPoint4"].ToString());
                    info.nExtPoint5 = Convert.ToInt16(t.Rows[0]["nExtPoint5"].ToString());
                    info.nExtPoint6 = Convert.ToInt16(t.Rows[0]["nExtPoint6"].ToString());
                    info.nExtPoint7 = Convert.ToInt16(t.Rows[0]["nExtPoint7"].ToString());
                    if (tsub != null)
                    {
                        info.Email = tsub.Rows[0]["cEMail"].ToString();
                        info.Phone = tsub.Rows[0]["cPhone"].ToString();
                        info.Quest = tsub.Rows[0]["cQuestion"].ToString();
                        info.Answer = tsub.Rows[0]["cAnswer"].ToString();
                    }

                }
                return info;
            }
            catch
            {
                return null;
            }
        }
        [WebMethod]
        public List<Account_Info> GetList()
        {
            List<Account_Info> list=  new List<Account_Info>();
            Account_Info info = null;
            DataTable t = common.ListAccount();
            if (t != null)
            {
                foreach (DataRow r in t.Rows)
                {
                    info = new Account_Info();
                    info.bIsBanned = bool.Parse(r["bIsBanned"].ToString());
                    info.bIsUseOTP = bool.Parse(r["bIsUseOTP"].ToString());
                    info.bParentalControl = bool.Parse(r["bParentalControl"].ToString());
                    info.cAccName = r["cAccName"].ToString();
                    if (r["dLoginDate"].ToString() == null)
                    {

                        info.dLoginDate = null;
                    }
                    else
                    {
                        info.dLoginDate = DateTime.Parse(r["dLoginDate"].ToString());
                    }
                    if (r["dLogoutDate"].ToString() == null)
                    {

                        info.dLogoutDate = null;
                    }
                    else
                    {
                        info.dLogoutDate = DateTime.Parse(r["dLogoutDate"].ToString());
                    }
                    info.nExtPoint = Convert.ToInt16(r["nExtPoint"].ToString());
                    info.nExtPoint1 = Convert.ToInt16(r["nExtPoint1"].ToString());
                    info.nExtPoint2 = Convert.ToInt16(r["nExtPoint2"].ToString());
                    info.nExtPoint3 = Convert.ToInt16(r["nExtPoint3"].ToString());
                    info.nExtPoint4 = Convert.ToInt16(r["nExtPoint4"].ToString());
                    info.nExtPoint5 = Convert.ToInt16(r["nExtPoint5"].ToString());
                    info.nExtPoint6 = Convert.ToInt16(r["nExtPoint6"].ToString());
                    info.nExtPoint7 = Convert.ToInt16(r["nExtPoint7"].ToString());
                    info.cAccName = r["cAccName"].ToString();
                    list.Add(info);
                }
            }
            return list;
        }
    }
}